package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallUser;
import com.cskaoyan.bean.bo.WXAuthLoginBO;
import com.cskaoyan.bean.bo.WxAuthRegisterBo;
import com.cskaoyan.bean.bo.WxAuthResetBo;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.WXAuthService;
import com.cskaoyan.shiro.LoginToken;
import com.cskaoyan.utils.IpUtils;
import com.cskaoyan.utils.PhoneUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;


@RequestMapping("/wx/auth")
@RestController
public class WXAuthController {

    @Autowired
    WXAuthService wxAuthService;

    private String mobile;
    private String code;

    @RequestMapping("/login")
    public BaseRespVo login(@RequestBody WXAuthLoginBO bo, HttpServletRequest req) {

        String username = bo.getUsername();
        String password = bo.getPassword();
        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)) {
            return BaseRespVo.fail(402);
        }

        Subject subject = SecurityUtils.getSubject();
        try {
            Md5Hash md5Hash = new Md5Hash(password);
            md5Hash.setIterations(1024);
            password = md5Hash.toHex();
            System.out.println(username + ": " + password);
            subject.login(new LoginToken(username, password, "wx"));
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            throw e;
        }

        username = (String) subject.getPrincipal();
        CskaoyanmallUser user = wxAuthService.selectUserByName(username);
        user.setLastLoginIp(IpUtils.getIpAddr(req));
        user.setLastLoginTime(new Date());
        wxAuthService.updateUser(user);
        Serializable token = subject.getSession().getId();
        UserLoginData userLoginData = new UserLoginData();
        userLoginData.setAvatarUrl(user.getAvatar());
        userLoginData.setNickName(user.getNickname());
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setToken(token);
        userLoginVo.setTokenExpire(new Date());
        userLoginVo.setUserInfo(userLoginData);
        return BaseRespVo.success(userLoginVo);
    }


    @RequestMapping("/logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.success();
    }

    @RequestMapping("/register")
    public BaseRespVo register(@RequestBody WxAuthRegisterBo bo, HttpServletRequest req) {


        if (ObjectUtils.isEmpty(bo.getCode()) || !bo.getCode().equals(code)) {
            return BaseRespVo.fail(703);
        }
        if (ObjectUtils.isEmpty(bo.getMobile()) || !bo.getMobile().equals(mobile)) {
            return BaseRespVo.fail(704);
        }
        String username = wxAuthService.selectUsernameByMobile(mobile);
        if (!ObjectUtils.isEmpty(username)) {
            return BaseRespVo.fail(650);
        }
        CskaoyanmallUser user = wxAuthService.selectUserByName(bo.getUsername());
        if (!ObjectUtils.isEmpty(user)) {
            return BaseRespVo.fail(650);
        }
        int effect = wxAuthService.insertUser(bo, req);
        if (effect == 1) {
            return BaseRespVo.success();
        } else {
            return BaseRespVo.fail(651);
        }
    }


    @RequestMapping("/reset")
    public BaseRespVo reset(@RequestBody WxAuthResetBo bo) {
        if (ObjectUtils.isEmpty(bo.getCode()) || !bo.getCode().equals(code)) {
            return BaseRespVo.fail(703);
        }
        if (ObjectUtils.isEmpty(bo.getMobile()) || !bo.getMobile().equals(mobile)) {
            return BaseRespVo.fail(704);
        }
        int effect = wxAuthService.updatePassword(bo);
        if (effect == 1) {
            return BaseRespVo.success();
        } else {
            return BaseRespVo.fail(652);
        }
    }


    @RequestMapping("/regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map<String, String> map) {
        mobile = map.get("mobile");
        code = PhoneUtil.validCode(mobile);
        System.out.println("code: " + code);
        return BaseRespVo.success();
    }
}
